package com.jushu.storbox.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.jushu.storbox.R;
import com.jushu.storbox.util.DirectoryUtil;
import com.jushu.storbox.util.OnImageDownload;
import com.jushu.storbox.util.image.ImageDownloader;
import com.jushu.storbox.view.viewImpl.MyImageView;

public class ChildAdapter extends BaseAdapter {
	private List<String> list;
	private ImageDownloader mDownloader;
	protected LayoutInflater mInflater;
	private ArrayList<String> selectedFiles = null;
	private Map<String, Boolean> checkBoxState;
	private Context context;
	private GridView gridView;
	public List<Boolean> isSelected;
	private Handler handler;

	public ChildAdapter(Context context, List<String> list, GridView gridView,
			Handler handler) {
		this.list = list;
		this.gridView = gridView;
		selectedFiles = new ArrayList<String>();
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.handler = handler;
		getIsSelected();
	}

	public void getIsSelected() {
		isSelected = new ArrayList<Boolean>();
		for (int i = 0; i < list.size(); i++) {
			isSelected.add(i, false);
		}
	}

	public ArrayList<String> getSelectedFiles() {
//		if (selectedFiles == null || isSelected == null) {
//			return new ArrayList<String>();
//		}
//		selectedFiles.clear();
//		for (int i = 0; i < isSelected.size(); i++) {
//			if (isSelected.get(i)) {
//				selectedFiles.add(list.get(i));
//			}
//		}
		return selectedFiles;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final String path = list.get(position);
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = ((LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
					.inflate(R.layout.grid_child_item, null);
			viewHolder = new ViewHolder();
			viewHolder.mParentView = convertView.findViewById(R.id.framelayout);
			viewHolder.mImageView = (MyImageView) convertView
					.findViewById(R.id.child_image);
			viewHolder.mCheckBox = (CheckBox) convertView
					.findViewById(R.id.child_checkbox);
			viewHolder.mImageView.setImageResource(R.mipmap.empty_pic);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.mImageView.setTag(path);
		if (mDownloader == null) {
			mDownloader = new ImageDownloader();
		}
		viewHolder.mImageView.setImageResource(R.mipmap.default_file_icon);
		if (mDownloader != null) {
			mDownloader.imageDownload(path, viewHolder.mImageView, null, 2,
					DirectoryUtil.getCurrentThumbnailPath(context), context, new OnImageDownload() {
						@Override
						public void onDownloadSucc(Bitmap bitmap, String c_url,
								ImageView mimageView) {
							ImageView imageView = (ImageView) gridView
									.findViewWithTag(c_url);

							if (imageView != null) {
								imageView.setImageBitmap(bitmap);
								imageView.setTag("");
							}
						}
					});
		}
		viewHolder.mParentView.setId(position);
		viewHolder.mCheckBox.setId(position);
		viewHolder.mParentView.setTag(viewHolder.mCheckBox);
		// viewHolder.mParentView.setOnClickListener(new OnClickListenerImpl(2,
		// position));
		
		if (isSelected.size() == 0) {
			getIsSelected();
		}
		 viewHolder.mCheckBox.setChecked(isSelected.get(position));
		// viewHolder.mCheckBox
		// .setOnClickListener(new OnClickListenerImpl(path, 2));
		// viewHolder.mCheckBox
		// .setOnCheckedChangeListener(new OnCheckedChangeListener() {
		// @Override
		// public void onCheckedChanged(CompoundButton buttonView,
		// boolean isChecked) {
		// isSelected.set(buttonView.getId(), isChecked);
		// // isSelected.set(position, isChecked);
		// }
		// });
		viewHolder.mParentView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				CheckBox checkBox = (CheckBox) v.getTag();
				String path = list.get(checkBox.getId());
				if (checkBox.isChecked()) {
					checkBox.setChecked(false);
					selectedFiles.remove(path);
				} else {
					if(selectedFiles.size() >= 10){
						Toast.makeText(
								context,
								context.getResources().getString(
										R.string.select_image_prompt),
								Toast.LENGTH_SHORT).show();
						checkBox.setChecked(false);
						return;
					}
					checkBox.setChecked(true);
					selectedFiles.add(path);
				}
				isSelected.set(v.getId(), checkBox.isChecked());

			}
		});
		return convertView;
	}

	public static class ViewHolder {
		public MyImageView mImageView;

		public CheckBox mCheckBox;

		private View mParentView;
	}

	public void clearCacheState() {
		if (null != checkBoxState) {
			checkBoxState.clear();
		}
		if (null != selectedFiles) {
			selectedFiles.clear();
		}
	}

	private class OnClickListenerImpl implements OnClickListener {
		private String file;
		private int viewTypeID;
		private int position;

		public OnClickListenerImpl(String file, int viewTypeID) {
			this.file = file;
			this.viewTypeID = viewTypeID;
		}

		public OnClickListenerImpl(int viewTypeID, int position) {
			this.viewTypeID = viewTypeID;
			this.position = position;
		}

		@Override
		public void onClick(View view) {
			if (viewTypeID == 1) {
				Bundle bundle = new Bundle();
				ArrayList<String> array = (ArrayList<String>) list;
				bundle.putStringArrayList("allImageUrl", array);
				bundle.putInt("position", position);
				Message message = new Message();
				message.what = 101;
				message.obj = bundle;
				handler.sendMessage(message);
			}
			if (viewTypeID == 2) {

				CheckBox checkBox = (CheckBox) view.getTag();
				if (!(checkBox).isChecked()) {
					checkBox.setChecked(false);
					selectedFiles.remove(file);
				} else {
					if (selectedFiles.size() < 10) {
						if (((CheckBox) view).isChecked()) {
							if (new File(file).length() > 0) {
								checkBox.setChecked(true);
								selectedFiles.add(file);
							} else {
								checkBox.setChecked(false);
							}
						} else if (!((CheckBox) view).isChecked()) {
							checkBox.setChecked(false);
							selectedFiles.remove(file);
						}
					} else {
						((CheckBox) view).setChecked(false);
						Toast.makeText(
								context,
								context.getResources().getString(
										R.string.select_image_prompt),
								Toast.LENGTH_SHORT).show();
					}
				}
			}
		}

	}
}
